﻿using Microsoft.Practices.Unity;
using SampleServiceInterfacesAndImplementation;
using System;

namespace UnitySamples
{
    /*
     * In addition to constructor injection, described earlier, 
     * the Unity Application Block supports property and method call injection. 
     * The following code demonstrates property injection. 
     * A class named ProductService exposes as a property a reference to an 
     * instance of another class named SupplierData (not defined in the 
     * following code). To force dependency injection of the dependent object, 
     * developers must apply the Dependency attribute to the property 
     * declaration, as shown in the following code.
     */
    public class ProductService
    {
        [Dependency]
        public SupplierData SupplierDetails
        {get;set;}
    } 

    public class Property_Injection
    {
        /*
         * Now, creating an instance of the ProductService class using the
         * Unity Application Block ** automatically ** generates an instance of the 
         * SupplierData class and sets it as the value of the SupplierDetails
         * property of the ProductService class.
        */
        public static void Sample()
        {

            //creating instance manually
            ProductService myInstanceManually = new ProductService();
            myInstanceManually.SupplierDetails = new SupplierData();
            //..

            //using unity
            IUnityContainer uContainer = new UnityContainer();
            ProductService myInstanceWithUnity = uContainer.Resolve<ProductService>();
            
            //test instances with unity
            //ProductService myInstance = new ProductService();//will not work
            Console.WriteLine("Supplier name " + myInstanceWithUnity.SupplierDetails.GetName());

        }
    }
}